package cn.mcch.investigator.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.mcch.investigator.R;
import cn.mcch.investigator.dialog.CarTypeChildListDialog;
import cn.mcch.investigator.dialog.LoadingDialog;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.bean.CarType;
import cn.mcch.investigator.model.impl.ChooseCarTypeModelImpl;
import cn.mcch.investigator.observer.InvestigatorObserverable;
import cn.mcch.investigator.presenter.impl.ChooseCarTypePresenterImpl;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.DialogUtils;
import cn.mcch.investigator.utils.JsonUtils;
import cn.mcch.investigator.viewdelegate.impl.ChooseCarTypeViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/20.
 */
public class ChooseCarTypeActivity extends BaseActivity<ChooseCarTypeViewDelegateImpl, ChooseCarTypeModelImpl, ChooseCarTypePresenterImpl> implements OnResponseListener {

    private LoadingDialog dialog;
    private CarTypeChildListDialog carTypeChildListDialog;

    private List<CarType> carTypeChildList;
    private CarType selectedCarList;
    private CarType selectedCarHot;

    @Override
    public Class<ChooseCarTypePresenterImpl> getPresenterClass() {
        return ChooseCarTypePresenterImpl.class;
    }

    @Override
    public void createFirstView() {
        mPresenter.createFirstView(getApplicationContext(), R.layout.layout_loading, this);
    }

    @Override
    public void initTitleBar() {
        mPresenter.initTitleBar(getApplicationContext(), isTranslucentStatus, "车型选择", R.drawable.back, 0, R.color.login_btn_text, this);
    }

    @Override
    public void initData() {
        Map<String, String> params = new LinkedHashMap<>();
        mPresenter.initData(ConstantUtil.ACT_CAR_TYEP, params, getApplicationContext(), this, R.layout.activity_choose_car_type);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        selectedCarList = mPresenter.childDatas.get(groupPosition).get(childPosition);
        dialog = DialogUtils.showLoadingDialog(getSupportFragmentManager(), "正在加载数据，请稍后...");
        mPresenter.getCarType2(1, selectedCarList.id, this);
        return false;
    }

    @Override
    public void onBefore(int id) {

    }

    @Override
    public void onAfter(boolean result, int id) {
        dialog.dismiss();
        dialog = null;
    }

    @Override
    public void onSuccess(String json, int id) {
        switch (id) {
            case 1:
            case 2:
                // 获取二级车型成功
                carTypeChildList = JsonUtils.fromJsonToList(json, CarType.class);
                if (carTypeChildList == null || carTypeChildList.size() == 0) {
                    // 二级车型为空,直接返回
                    this.finish();
                    if (id == 1) {
                        InvestigatorObserverable.notifyObservers(100, selectedCarList);
                    } else {
                        InvestigatorObserverable.notifyObservers(300, selectedCarHot);
                    }

                } else {
                    // 弹出选择二级车型对话框
                    mode = modeChildClick;
                    carTypeChildListDialog = DialogUtils.showCarTypeChildListDialog(getSupportFragmentManager(), this, carTypeChildList);
                }
                break;
            default:
                break;
        }
    }

    private int mode;
    private int modeChildClick = 1;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (mode == modeChildClick) {
            carTypeChildListDialog.dismiss();
            carTypeChildListDialog = null;
            // 二级车型不为空
            this.finish();
            InvestigatorObserverable.notifyObservers(200, carTypeChildList.get(position));
        } else {
            selectedCarHot = mPresenter.carTypeBean.hot.get(position);
            dialog = DialogUtils.showLoadingDialog(getSupportFragmentManager(), "正在加载数据，请稍后...");
            mPresenter.getCarType2(2, selectedCarHot.id, this);
        }

    }
}
